package com.xianbeiying.app.ckgl.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

/**
 * 手机号处理工具类
 */
public class PhoneUtil {
    public static final int PHONE_LENGTH = 11;
    //    public static final String REG_PHONE = "^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$";
    public static final String REG_PHONE = "^((13[0-9])|(147)|(15[^4,\\D])|(18[^4])|(17[0,0-9]))\\d{8}$";
    private static final int HIDE_START_INDEX = 3;
    private static final int HIDE_END_INDEX = 7;
    private static final String HIDE_FORMAT = "%s****%s";

    public static String hide(String phone) {
        if (isNotPhone(phone)) {
            return null;
        }
        return String.format(HIDE_FORMAT, phone.substring(0, HIDE_START_INDEX), phone.substring(HIDE_END_INDEX, PHONE_LENGTH));
    }

    public static boolean isNotPhone(String phone) {
        return !isPhone(phone);
    }

    public static boolean isPhone(String phone) {
        return RegUtil.matcher(phone, REG_PHONE);
    }

    public static void showCallDialog(final Context context, final String tel) {
        if (context == null || TextUtils.isEmpty(tel)) {
            return;
        }
        new AlertDialog.Builder(context)
                .setTitle("确认")
                .setMessage(String.format("要打电话给%s吗？", tel))
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        onCall(context, tel);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {
                        dialog.dismiss();
                    }
                })
                .create().show();
    }

    public static void onCall(Context context, String tel) {
        try {
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + tel));
            //context.startActivity(intent);
        } catch (Exception e) {
//            com.lidroid.xutils.util.LogUtils.d("拨打电话失败");
        }
    }

    public static void onSendSMS(Context context, String phone) {
        Uri uri = Uri.parse("smsto:" + phone);
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        context.startActivity(intent);
    }
}
